package com.hxzy.controller.common;

import cn.hutool.core.lang.UUID;
import com.hxzy.common.constant.CacheConstants;
import com.hxzy.common.core.domain.R;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Api(tags = "图片验证码")
@RestController
@RequestMapping(value = "/common/captcha")
public class CaptchaController {


    @Autowired
    private RedisTemplate  redisTemplate;

    /**
     * 生成png格式
     * @param width
     * @param height
     * @param len
     * @return
     */
    @ApiOperation(value = "png图片验证码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "width",value = "图片宽度",defaultValue = "150"),
            @ApiImplicitParam(name = "height",value = "图片高度",defaultValue = "50"),
            @ApiImplicitParam(name = "len",value = "验证码长度",defaultValue = "4"),
    })
    @GetMapping(value = "/png")
    public R png(@RequestParam(value = "width",defaultValue = "150") int width,
                 @RequestParam(value = "height",defaultValue = "50") int height,
                 @RequestParam(value = "len",defaultValue = "4") int len){

        //1、生成验证码
        SpecCaptcha captcha = new SpecCaptcha(width, height);
        //验证码长度
        captcha.setLen(len);
        // 获取验证码的字符
        String value=captcha.text();
        // 把图片转换成base64加密的图片字符串
        String imgSrc=captcha.toBase64();

        //2、获取redis的key
        String  uuid = UUID.fastUUID().toString();
        String redisKey= CacheConstants.CAPTCHA_CODE_KEY+ uuid;

        //3、存入到redis中，并且加上过期时间
        this.redisTemplate.opsForValue().set( redisKey,value, 5, TimeUnit.MINUTES);


        //3、把数据返回给前端
        Map<String,Object> data=new HashMap<>();
        data.put("uuid", uuid);
        data.put("img", imgSrc);
        return R.okHasData(data);
    }


    /**
     * 生成gif格式
     * @param width
     * @param height
     * @param len
     * @return
     */
    @ApiOperation(value = "gif图片验证码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "width",value = "图片宽度",defaultValue = "150"),
            @ApiImplicitParam(name = "height",value = "图片高度",defaultValue = "50"),
            @ApiImplicitParam(name = "len",value = "验证码长度",defaultValue = "4"),
    })
    @GetMapping(value = "/gif")
    public R gif(@RequestParam(value = "width",defaultValue = "150") int width,
                 @RequestParam(value = "height",defaultValue = "50") int height,
                 @RequestParam(value = "len",defaultValue = "4") int len){

        //1、生成验证码
        GifCaptcha captcha = new GifCaptcha(width, height);
        //验证码长度
        captcha.setLen(len);
        // 获取验证码的字符
        String value=captcha.text();
        // 把图片转换成base64加密的图片字符串
        String imgSrc=captcha.toBase64();

        //2、获取redis的key
        String  uuid = UUID.fastUUID().toString();
        String redisKey= CacheConstants.CAPTCHA_CODE_KEY+ uuid;

        //3、存入到redis中，并且加上过期时间
        this.redisTemplate.opsForValue().set( redisKey,value, 5, TimeUnit.MINUTES);


        //3、把数据返回给前端
        Map<String,Object> data=new HashMap<>();
        data.put("uuid", uuid);
        data.put("img", imgSrc);
        return R.okHasData(data);
    }

    /**
     * 生成算术运算格式
     * @param width
     * @param height
     * @param len
     * @return
     */
    @ApiOperation(value = "算术运算图片验证码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "width",value = "图片宽度",defaultValue = "150"),
            @ApiImplicitParam(name = "height",value = "图片高度",defaultValue = "50"),
            @ApiImplicitParam(name = "len",value = "验证码长度",defaultValue = "2"),
    })
    @GetMapping(value = "/airth")
    public R airth(@RequestParam(value = "width",defaultValue = "150") int width,
                 @RequestParam(value = "height",defaultValue = "50") int height,
                 @RequestParam(value = "len",defaultValue = "2") int len){

        //1、生成验证码
        ArithmeticCaptcha captcha = new ArithmeticCaptcha (width, height);
        //验证码长度
        captcha.setLen(len);
        // 获取验证码的字符
        String value=captcha.text();
        // 把图片转换成base64加密的图片字符串
        String imgSrc=captcha.toBase64();

        //2、获取redis的key
        String  uuid = UUID.fastUUID().toString();
        String redisKey= CacheConstants.CAPTCHA_CODE_KEY+ uuid;

        //3、存入到redis中，并且加上过期时间
        this.redisTemplate.opsForValue().set(redisKey,value, 5, TimeUnit.MINUTES);


        //3、把数据返回给前端
        Map<String,Object> data=new HashMap<>();
        data.put("uuid",uuid);
        data.put("img", imgSrc);
        return R.okHasData(data);
    }

}
